#include "ExitConfirmPopup.h"
#include "CCDirector.h"
#include "CCApplication.h"

bool ExitConfirmPopup::isShowing = false;

bool ExitConfirmPopup::init()
{
	screenSize = CCDirector::sharedDirector()->getWinSize();

	initBackgroundPopup();
    
    return true;
}

void ExitConfirmPopup::initBackgroundPopup()
{
	CCLayerColor* shadowLayer = CCLayerColor::create(ccc4(0, 0, 0, 150), screenSize.width, screenSize.height);
	shadowLayer->setAnchorPoint(CCPointZero);
	shadowLayer->setPosition(ccp(-screenSize.width * 0.5, -screenSize.height * 0.5));
	this->addChild(shadowLayer);

	CCSprite* bgPopup = CCSprite::create("back_ground_popup.png");
	bgPopup->setAnchorPoint(ccp(0.5f, 0.5f));
	bgPopup->setPosition(CCPointZero);
	this->addChild(bgPopup);

	initButton(bgPopup);
	initAnimationPopup(bgPopup);

	isShowing = true;
}

void ExitConfirmPopup::initButton(CCSprite* bgPopup)
{
	CCMenuItemImage* menuExit = CCMenuItemImage::create("bt_yes.png", "bt_yes_focus.png", this, menu_selector(ExitConfirmPopup::exitClickListener));
	menuExit->setAnchorPoint(ccp(0.5f, 0.5f));
	menuExit->setPosition(ccp(bgPopup->getContentSize().width*0.5 - screenSize.width*0.12, bgPopup->getContentSize().height*0.2));
	
	CCMenuItemImage* menuCancel = CCMenuItemImage::create("bt_no.png", "bt_no_focus.png", this, menu_selector(ExitConfirmPopup::cancelClickListener));
	menuCancel->setAnchorPoint(ccp(0.5f, 0.5f));
	menuCancel->setPosition(ccp(bgPopup->getContentSize().width*0.5 + screenSize.width*0.12, bgPopup->getContentSize().height*0.2));

	CCMenu* menu = CCMenu::create(menuExit, menuCancel, NULL);
	menu->setAnchorPoint(ccp(0.5f, 0.5f));
	menu->setPosition(CCPointZero);
	bgPopup->addChild(menu);
}

void ExitConfirmPopup::initContent(char* content, CCSprite* bgPopup)
{
	CCLabelTTF* lblContent = CCLabelTTF::create(content , "fonts/arial.ttf", screenSize.width*0.05);
	lblContent->setAnchorPoint(ccp(0.5f, 0.5f));
	lblContent->setPosition(ccp(bgPopup->getContentSize().width * 0.5f, bgPopup->getContentSize().height * 0.5f));
	bgPopup->addChild(lblContent);
}

void ExitConfirmPopup::initAnimationPopup(CCSprite* bgPopup)
{
	CCAction* popupAnimation = CCSequence::create(CCScaleTo::create(0.0, 0.0),
                                              CCScaleTo::create(0.06, 1.05),
                                              CCScaleTo::create(0.08, 0.95),
                                              CCScaleTo::create(0.08, 1.0), NULL);
    bgPopup->runAction(popupAnimation);
}

void ExitConfirmPopup::exitClickListener(CCObject* pSender)
{
	CCDirector::sharedDirector()->end();
}

void ExitConfirmPopup::cancelClickListener(CCObject* pSender)
{
	isShowing = false;
	this->removeFromParentAndCleanup(true);
}